﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.SocialPlatforms.Impl;
using UnityEngine.UI;

namespace MGP_004CompoundBigWatermelon
{ 

	public class UIManager : IManager
	{
        private Text m_ScoreText;
        private GameObject m_GameOverImageGo;
        private Button m_RestartGameButton;

        private ScoreManager m_ScoreManager;
        public void Init(Transform worldTrans, Transform uiTrans, params object[] manager)
        {
            m_ScoreText = uiTrans.Find(GameObjectPathInSceneDefine.UI_SCORE_TEXT_PATH).GetComponent<Text>();
            m_GameOverImageGo = uiTrans.Find(GameObjectPathInSceneDefine.UI_GAME_OVER_IMAGE_PATH).gameObject;
            m_RestartGameButton = uiTrans.Find(GameObjectPathInSceneDefine.UI_RESTART_GAME_BUTTON_PATH).GetComponent<Button>();

            m_ScoreManager = manager[0] as ScoreManager;

            m_GameOverImageGo.SetActive(false);
            m_ScoreText.text = m_ScoreManager.Score.ToString();
            m_ScoreManager.OnValueChanged += OnScroeValueChanged;
            m_RestartGameButton.onClick.AddListener(OnRestartButton);
        }

        public void Update()
        {
            
        }

        public void Destroy()
        {
            m_ScoreManager.OnValueChanged -= OnScroeValueChanged;
            m_RestartGameButton.onClick.RemoveAllListeners();

            m_ScoreText = null;
            m_GameOverImageGo = null;
            m_RestartGameButton = null;
            m_ScoreManager = null;
        }

        public void OnGameOver() {
            m_GameOverImageGo.SetActive(true);
        }

        private void OnScroeValueChanged(int score) {
            m_ScoreText.text = score.ToString();
        }

        private void OnRestartButton() {
            SceneManager.LoadScene(SceneManager.GetActiveScene().name);
        }

        public void TouchWatchAds()
        {
            string videoAdid = VideData.Instance.GetVide();
            TTSDK.TTRewardedVideoAd r = AdMgr.Instance.ShowVideoAdNew(videoAdid);
            //m_ScoreManager.Score += 10;
            //m_ScoreText.text = m_ScoreManager.Score.ToString();
            //创建激励视频关闭回调
            r.OnClose += (bol, val) =>
            {
                Debug.Log("关闭广告 ，是否已经看完" + bol);
                //看完广告获得提示
                if (bol)
                {
                    m_ScoreManager.Score += 10;
                    m_ScoreText.text = m_ScoreManager.Score.ToString();
                    MaidianNew.MaidianBackCallback(" ");
                    Debug.Log("广告看完");
                }
                else
                {
                    Debug.Log("广告未看完");
                }
            };

            //创建报错回调  
            r.OnError += (code, errorMessage) =>
            {

                Debug.Log("code:" + code + "错误信息：" + errorMessage);
            };

            //展示广告（新版本必须调用，不然无法展示出来广告）
            r.Show();

        }
    }
}
